package com.broada.apm.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.broada.apm.indicator.api.model.Variable;
import com.broada.apm.model.ConfigItem;

/**
 * 
 * 常量类，放置配置保存主键id键值对
 * 
 * @author xiangsy Create By 2015年1月12日 上午11:19:53
 */
public class SettingMapConstants {
	public static Map<String, String> SETTINGSMAP = new HashMap<String, String>();

	static {
		// Apdex满意值T（秒）
		SETTINGSMAP.put("basic.apdex_t", "f3daad27-8e26-4cac-8066-8062c1e121de");
		// 数据保存期限（天）
//		SETTINGSMAP.put("basic.keep_time", "ebbca394-275f-41f3-a284-22b9deadb758");
		// 基本配置LOGO
//		SETTINGSMAP.put("basic.logo", "cace5bd0-3b4e-41ab-b6f1-a625eb5bc9d9");
		// 集成配置启用
		SETTINGSMAP.put("integrate.coss.enabled", "c3286788-ed8c-4a0c-b897-da512aa5774a");
		// coss服务IP
		SETTINGSMAP.put("integrate.coss.ip", "bcf2537f-f4ae-4969-85b0-1075f85a5f70");
		// coss服务端口
		SETTINGSMAP.put("integrate.coss.port", "041bd243-bf4e-4a8b-8eed-34ebe33dc2f9");
		// 告警集成方式
		SETTINGSMAP.put("integrate.alert.trap_enabled", "14a7b024-4fb7-443b-97b2-53de2d1ce4b3");
		// 告警平台的IP
		SETTINGSMAP.put("integrate.alert.ip", "1ef6cb74-6060-4272-87bf-691121f98446");
		// 告警平台的端口
		SETTINGSMAP.put("integrate.alert.port", "2c4c881d-52c4-43a6-93ff-c7816328afac");
		// 集成配置统一用户认证启用
		SETTINGSMAP.put("integrate.sso.enabled", "2e24a537-8b78-4490-a2aa-e5b662c4abd0");
		// 统一用户服务地址
		SETTINGSMAP.put("integrate.sso.ip", "091db807-e2d3-47a4-a8b5-b76dfeea63e9");
		// 统一用户服务端口
		SETTINGSMAP.put("integrate.sso.port", "93408620-f0fb-4d63-8127-d2ea280d7328");
		// 服务配置主机名称
		SETTINGSMAP.put("server.basic.host_name", "7d944188-6088-492e-9c97-49b5c79a5ba4");
		// 服务配置本机IP
		SETTINGSMAP.put("server.basic.ip", "9db73578-5929-4448-8307-4b61a74cd24c");
		// 服务配置子网掩码
		SETTINGSMAP.put("server.basic.netmask", "f59dd8fc-fdaf-4a85-801e-f3b663727948");
		// 服务配置网关
		SETTINGSMAP.put("server.basic.gateway", "dc3b8773-2cd5-46fb-a39d-d83b69b479eb");
		// 服务配置首选DNS服务器
		SETTINGSMAP.put("server.basic.dns1", "c1330b35-f3ba-4dd8-832f-73b4f9ba331a");
		// 服务配置备用DNS服务器
		SETTINGSMAP.put("server.basic.dns2", "ef7dc3a8-36d1-4bc4-8b28-3d34f47bb60a");
		// 服务配置时间服务器
		SETTINGSMAP.put("server.time.ntp_server", "610927b3-0c9d-43d1-ac90-f76cd616bd5e");
		// 服务配置自动同步
		SETTINGSMAP.put("server.time.auto_sync", "d218d7b9-67c5-4796-8230-b7e3f6fc83d6");
		// 服务配置WEB访问端口
		SETTINGSMAP.put("service.web.port", "7e80b15b-489e-40d4-96f4-c42bc88f7077");
		// 服务配置启用本机APM指标计算服务
		SETTINGSMAP.put("service.indicator.enabled", "9d033318-f9af-4a99-ad7a-50a4d5e8d8a2");
		// 服务配置远程指标计算服务地址
		SETTINGSMAP.put("service.indicator.remote_ip", "b74b48e7-43d7-47e4-8927-9fa4f38b9f69");
		// 服务配置端口
		SETTINGSMAP.put("service.indicator.remote_port", "445efbc8-d0b7-4435-a578-0f5a0fd50905");
		// 数据包过滤缺省策略
		SETTINGSMAP.put("filter.default_policy", "filter.default_policy");
		// 数据包过滤规则
		SETTINGSMAP.put("filter.rules", "filter.rules");
		// 地理位置信息配置启用内置库
		SETTINGSMAP.put("map.internal.enabled", "4a52e369-3aa1-4324-9732-45c7089cf5ec");
		// 地理位置信息配置使用地图
		SETTINGSMAP.put("map.to_use", "73c651e7-0f12-4e08-9ce4-9f0c12392cac");
		// 地理位置信息配置内置库自动更新
		SETTINGSMAP.put("map.internal.auto_sync", "8b8c2778-a021-464c-ae78-9b2ca9458f3c");
		// 设置历史数据清理时间
		SETTINGSMAP.put("history.cleantime", "d5b95062-e08d-421d-a0be-c47f2296e843");

	};

	/**
	 * 
	 * @param web前端传过来的带有配置值的map
	 * @param confType 配置类型标识值
	 * @return 保存方法参数所需map类型
	 */
	public static Variable[] getConfData(Map<String, String> map, int confType) {
		Variable[] confVar=null;
		if (map.size() > 0) {
			switch (confType) {
			//1,基本配置;2,集成配置;3,服务器配置;4,数据包过滤规则;5,全局告警配置;6,地理位置信息配置;7,L7模块;8,用户管理;
			//9,集群配置;10,历史数据管理;11,日志分析;12,系统监控;13,配置管理;14,系统升级;
			case 1:
				confVar=new Variable[1];
				Variable varApdex=confVar("basic.apdex_t",map.get("apdex_t"));
//				Variable varKeeptime=confVar("basic.keep_time",map.get("keepTime"));
				confVar[0]=varApdex;
//				confVar[1]=varKeeptime;
				break;
//			case 2:
//				confVar=new Variable[7];
//				Variable varCossEnable=confVar("integrate.coss.enabled",map.get("cossEnable"));
//				Variable varCossIp = confVar("integrate.coss.ip",map.get("cossIp"));
//				Variable varCossPort = confVar("integrate.coss.port",map.get("cossPort"));
//				Variable varAlarmEnable=confVar("integrate.alert.trap_enabled",map.get("alarmEnable"));
//				Variable varAlarmIp = confVar("integrate.alert.ip",map.get("alarmIp"));
//				Variable varAlarmPort = confVar("integrate.alert.port",map.get("alarmPort"));
//				Variable varCertificationEnable = confVar("integrate.sso.enabled",map.get("certificationEnable"));
//				confVar[0]=varCossEnable;
//				confVar[1]=varCossIp;
//				confVar[2]=varCossPort;
//				confVar[3]=varAlarmEnable;
//				confVar[4]=varAlarmIp;
//				confVar[5]=varAlarmPort;
//				confVar[6]=varCertificationEnable;
//				break;
			case 3:
				confVar=new Variable[12];
				Variable varMachineName=confVar("server.basic.host_name",map.get("machineName"));
				Variable varLocalIp = confVar("server.basic.ip",map.get("localIp"));
				Variable varSubnetMask = confVar("server.basic.netmask",map.get("subnetMask"));
				Variable varGateway=confVar("server.basic.gateway",map.get("gateway"));
				Variable varFirstDNS = confVar("server.basic.dns1",map.get("firstDNS"));
				Variable varSecondDNS = confVar("server.basic.dns2",map.get("secondDNS"));
				Variable varAutoSync=confVar("server.time.auto_sync",map.get("autoSync"));
				Variable varWebPort = confVar("service.web.port",map.get("webPort"));
				Variable varApmServerEnable = confVar("service.indicator.enabled",map.get("apmServerEnable"));
				Variable varRemoteIp=confVar("service.indicator.remote_ip",map.get("remoteIp"));
				Variable varTimeServer = confVar("server.time.ntp_server",map.get("timeServer"));
				Variable varPort = confVar("service.indicator.remote_port",map.get("port"));
				confVar[0]=varMachineName;
				confVar[1]=varLocalIp;
				confVar[2]=varSubnetMask;
				confVar[3]=varGateway;
				confVar[4]=varFirstDNS;
				confVar[5]=varSecondDNS;
				confVar[6]=varAutoSync;
				confVar[7]=varWebPort;
				confVar[8]=varApmServerEnable;
				confVar[9]=varRemoteIp;
				confVar[10]=varTimeServer;
				confVar[11]=varPort;
				break;
			case 4:
				confVar=new Variable[2];
				Variable varDefaultpolicys=confVar("filter.default_policy",map.get("defaultpolicy"));
				Variable varPolicyRules = confVar("filter.rules",map.get("policyRules"));
				confVar[0]=varDefaultpolicys;
				confVar[1]=varPolicyRules;
				break;
			case 5:
				confVar=new Variable[3];
				Variable varSnmpOn=confVar("integrate.alert.trap_enabled",map.get("snmpOn"));
				Variable varSnmpIp = confVar("integrate.alert.ip",map.get("snmpIp"));
				Variable varSnmpPort = confVar("integrate.alert.port",map.get("snmpPort"));
				confVar[0]=varSnmpOn;
				confVar[1]=varSnmpIp;
				confVar[2]=varSnmpPort;
				break;
			case 6:
				confVar=new Variable[2];
				Variable varIsuse=confVar("map.internal.enabled",map.get("isDedicate"));
				Variable varIsUpdate = confVar("map.internal.auto_sync",map.get("isUpdate"));
//				Variable varMapType = confVar("map.to_use",map.get("mapType"));
				confVar[0]=varIsuse;
				confVar[1]=varIsUpdate;
//				confVar[1]=varMapType;
				break;
			case 10:
				confVar=new Variable[1];
				Variable varCleanTime = confVar("history.cleantime",map.get("cleanTime"));
				confVar[0]=varCleanTime;
				break;
			}
		}
		return confVar;
	}

	/**
	 * @param confKey 为es中要修改字段的key
	 * @param confValue 前台传过来的值
	 * @return
	 */
	public static Variable confVar(String confKey,String confValue){
		Map<String, Object> map= new HashMap<String, Object>();
		if (confValue != null && confValue != ""){
			map.put("value", confValue);
		}else{
			map.put("value", "");
		}
		return Variable.newBuilder().setId(SETTINGSMAP.get(confKey)).setItem(map);
	}
	
	/**
	 * 将数据库中字段转换成前台显示字段
	 */
	public static Map<String,Object> resultFormat(List<Map> listMap){
		Map<String,Object> formatMap=new HashMap<String,Object>();
		for(Map map:listMap){
			String key=map.get("key").toString().replace(".", "_");
			formatMap.put(key, map.get("value"));
		}
		return formatMap;
	}
	/**
	 * 将数据库中字段转换成前台显示字段
	 */
	public static Map<String, Object> toMap(List<ConfigItem> settings) {
		Map<String, Object> formatMap = new HashMap<String, Object>();
		if (settings != null && !settings.isEmpty()){
			for (ConfigItem set : settings) {
				String key = set.getKey().replace(".", "_");
				formatMap.put(key, set.getValue());
			}
		}
		return formatMap;
	}
	
}
